package com.fumin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fumin.Filtter.BussinessException;
import com.fumin.pojo.*;
import com.fumin.service.CapitalService;
import com.fumin.service.ProjectsService;
import com.fumin.service.UserService;
import com.fumin.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * Created by Lingo on 2018/3/16.
 */
@Controller
public class SkipController {

    @Resource
    ProjectsService projectsService;
    @Resource
    UserService userService;
    @Resource
    CapitalService capitalService;

    private static String secrete = "WOCAONIMAGEBIgannilao123";
    private static String salt = "xxx";

    /**
     *  项目列表跳转
     */
    @RequestMapping(value = "/projectsSkip")
    public String projectSkip(String user,String timsTamp,String auth,String name,Integer num, Integer page, Model model){


        num = num==null?5:num;
        page = page==null?1:page;
        Integer start = num*(page-1);
        Integer offset = num;
        name = name==null|| "".equals(name)?name:"%"+name+"%";
        model.addAttribute("running",projectsService.selectNumByStatusAndName(0,name).get("num"));
        model.addAttribute("finish",projectsService.selectNumByStatusAndName(1,name).get("num"));
        model.addAttribute("ready",projectsService.selectNumByStatusAndName(2,name).get("num"));
        model.addAttribute("runningProjects",projectsService.selectPageByStatusAndName(start,offset,0,name));
        model.addAttribute("finishProjects",projectsService.selectPageByStatusAndName(start,offset,1,name));
        List<Projects> projects = projectsService.selectPageByStatus(start,offset,2);
        model.addAttribute("readyProjects",projectsService.selectPageByStatusAndName(start,offset,2,name));
        List<Projects> top4 = new LinkedList<>();
        for (int i = 0; i <4 ; i++) {
            top4.add(projects.get(i));
        }
        model.addAttribute("top4",top4);
        return "browser";
    }


    /**
     *  项目列表跳转
     */
    @RequestMapping(value = "/project")
    public String projectDetailSkip(Long project,Model model){
        ProjectsWithBLOBs projects = projectsService.selectByPrimaryKey(project);
        model.addAttribute("project",projects);
        return "project";
    }

    /**
     *  个人中心首页跳转
     */
    @RequestMapping(value = "/profileSkip",method = RequestMethod.GET)
    public String profileSkip(Long pk,String time,String auth, Model model){
        List<Capital> capitals = capitalService.selectByUserPk(pk);
        if(capitals==null|| capitals.size()==0){
            throw new BussinessException("-1","无效的请求,用户注册出现错误");
        }

        String user = pk.toString();
        String timeStamp = time;
        if (StringUtil.isNull(user) || StringUtil.isNull(timeStamp)){
            throw  new BussinessException("-1","无效的请求，加密参数缺失");
        }
        String tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        if (!tocken.equals(auth)){
            throw new BussinessException("-1","无效的请求，加密结果不正确");
        }

        Date date =  new Date();
        timeStamp = String.valueOf(date.getTime());
        tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        System.out.println(tocken);
        model.addAttribute("user",user);
        model.addAttribute("time",timeStamp);
        model.addAttribute("auth",tocken);
        return "profile";
    }

    /**
     *  充值页面跳转
     */
    @RequestMapping(value = "/deposeSkip")
    public String deposeSkip(Long pk,String time,String auth, Model model){

        List<Capital> capitals =  capitalService.selectByUserPk(pk);

        String user = pk.toString();
        String timeStamp = time;
        if (StringUtil.isNull(user) || StringUtil.isNull(timeStamp)){
            throw  new BussinessException("-1","无效的请求");
        }
        String tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        if (!tocken.equals(auth)){
            throw new BussinessException("-1","无效的请求");
        }

        Date date =  new Date();
        timeStamp = String.valueOf(date.getTime());
        tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        model.addAttribute("user",user);
        model.addAttribute("time",timeStamp);
        model.addAttribute("auth",tocken);
        model.addAttribute("addr",capitals.get(0).getAccount_addr());
        return "deposit";
    }


    /**
     *  退款页面跳转
     */
    @RequestMapping(value = "/withdrawsSkip")
    public String withdrawsSkip(Long pk,String time,String auth, Model model){

        User userData = userService.selectByPrimaryKey(pk);
        List<Capital> capitals = capitalService.selectByUserPk(userData.getPk());
        Float amount = capitalService.getBlanceByAddr(capitals.get(0).getAccount_addr());


        String user = pk.toString();
        String timeStamp = time;
        if (StringUtil.isNull(user) || StringUtil.isNull(timeStamp)){
            throw  new BussinessException("-1","无效的请求");
        }
        String tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        if (!tocken.equals(auth)){
            throw new BussinessException("-1","无效的请求");
        }
        Date date =  new Date();
        timeStamp = String.valueOf(date.getTime());
        tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        model.addAttribute("user",user);
        model.addAttribute("time",timeStamp);
        model.addAttribute("auth",tocken);
        model.addAttribute("amount",amount);
        return "withdraws";
    }


    /**
     *  投资页面跳转
     */
    @RequestMapping(value = "/ordersSkip")
    public String ordersSkip(Long pk,String time,String auth, Model model){
        String user = pk.toString();
        String timeStamp = time;
        if (StringUtil.isNull(user) || StringUtil.isNull(timeStamp)){
            throw  new BussinessException("-1","无效的请求");
        }
        String tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        if (!tocken.equals(auth)){
            throw new BussinessException("-1","无效的请求");
        }
        Date date =  new Date();
        timeStamp = String.valueOf(date.getTime());
        tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        model.addAttribute("user",user);
        model.addAttribute("time",timeStamp);
        model.addAttribute("auth",tocken);
        return "orders";
    }

    /**
     *  投资页面跳转
     */
    @RequestMapping(value = "/transactionsSkip")
    public String transactionsSkip(Long pk,String time,String auth, Model model){
        String user = pk.toString();
        String timeStamp = time;
        if (StringUtil.isNull(user) || StringUtil.isNull(timeStamp)){
            throw  new BussinessException("-1","无效的请求");
        }
        String tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        if (!tocken.equals(auth)){
            throw new BussinessException("-1","无效的请求");
        }
        Date date =  new Date();
        timeStamp = String.valueOf(date.getTime());
        tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        model.addAttribute("user",user);
        model.addAttribute("time",timeStamp);
        model.addAttribute("auth",tocken);
        return "transactions";
    }

    /**
     *  投资页面跳转
     */
    @RequestMapping(value = "/supportSkip")
    public String supportSkip(Long pk,String time,String auth, Model model){
        String user = pk.toString();
        String timeStamp = time;
        if (StringUtil.isNull(user) || StringUtil.isNull(timeStamp)){
            throw  new BussinessException("-1","无效的请求");
        }
        String tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        if (!tocken.equals(auth)){
            throw new BussinessException("-1","无效的请求");
        }
        Date date =  new Date();
        timeStamp = String.valueOf(date.getTime());
        tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        model.addAttribute("user",user);
        model.addAttribute("time",timeStamp);
        model.addAttribute("auth",tocken);
        return "support";
    }



    /**
     *  投资页面跳转
     */
    @RequestMapping(value = "/placeorderSkip")
    public String placeorderSkip(Long pk,String time,String auth,Long pid,String pname , Model model){
        String user = pk.toString();
        String timeStamp = time;
        if (StringUtil.isNull(user) || StringUtil.isNull(timeStamp)){
            throw  new BussinessException("-1","无效的请求");
        }
        String tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        if (!tocken.equals(auth)){
            throw new BussinessException("-1","无效的请求");
        }
        Date date =  new Date();
        timeStamp = String.valueOf(date.getTime());
        tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        model.addAttribute("user",user);
        model.addAttribute("time",timeStamp);
        model.addAttribute("auth",tocken);

        Projects projects = projectsService.selectByPrimaryKey(pid);
        model.addAttribute("project",projects);
        return "placeorder";
    }

    /**
     *  调整
     */
    @RequestMapping(value = "/ethWithdrawSkip")
    public String ethWithdrawSkip(Long pk,String time,String auth, Model model){
        User userInstance = userService.selectByPrimaryKey(pk);
        List<Capital> results = capitalService.selectByUserPk(pk);
        for (Capital capital:results) {
            capital.setAmount(capitalService.getBlanceByAddr(capital.getAccount_addr()));
        }


        String user = pk.toString();
        String timeStamp = time;
        if (StringUtil.isNull(user) || StringUtil.isNull(timeStamp)){
            throw  new BussinessException("-1","无效的请求");
        }
        String tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        if (!tocken.equals(auth)){
            throw new BussinessException("-1","无效的请求");
        }
        Date date =  new Date();
        timeStamp = String.valueOf(date.getTime());
        tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
        model.addAttribute("user",user);
        model.addAttribute("time",timeStamp);
        model.addAttribute("auth",tocken);
        model.addAttribute("amount",results.get(0).getAmount());
        model.addAttribute("mobile",userInstance.getMobile_phone());
        return "ethWithdraw";
    }





    /**
     * 项目发布
     */
    @RequestMapping(value = "/slottingRequestSkip")
    public String slottingRequestSkip(){
        return "slottingRequest";
    }

    @RequestMapping(value = "/signin2Skip")
    public String loginSkip(Model model){
        return "signin2";
    }

    @RequestMapping(value = "/about")
    public String aboutusSkip(){
        return "about";
    }

    @RequestMapping(value = "/submit")
    public String tutorialSkip(){
        return "submit";
    }


    @RequestMapping(value = "/invite")
    public String inviteSkip(){
        return "invite";
    }

    @RequestMapping(value = "/process")
    public String processSkip(){
        return "process";
    }

    @RequestMapping(value = "/teams")
    public String teamsSkip(){
        return "teams";
    }






    public static String getRandomChar(int length) {
        char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(chr[random.nextInt(62)]);
        }
        return buffer.toString();
    }



    
    
    


}
